<?php

/**
 * captcha actions.
 *
 * @package    jiazhang
 * @subpackage captcha
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class captchaActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeGet_sms(sfWebRequest $request)
  {
      sfConfig::set('sf_web_debug',false);
    if($request->isMethod('POST')){
        $phone = $request->getParameter('phone',0);
        $type = (int)$request->getParameter('type',0);
        $msg = array('error'=>0,'msg'=>'');
        
        if(strlen($phone) != 11){
          $msg['error'] = 1;
          $msg['msg'] = '手机号码格式错误！请重新填写';
          return $this->renderText(json_encode($msg));
        }else{
           
           $has_user = Doctrine::getTable('User')->findOneByPhone($phone); 
           
           if($type == 0 && $has_user){  //注册验证码，判断是否已经注册的手机
              $msg['error'] = 1;
              $msg['msg'] = '手机号已经注册！请重新填写';
              return $this->renderText(json_encode($msg));
          }elseif($type == 1 && (!$has_user)){  //找回密码验证码，判断是否已经存在的手机
              $msg['error'] = 1;
              $msg['msg'] = '手机号没有注册！请重新填写';
              return $this->renderText(json_encode($msg));
          }
          
          $code = rand(111111, 999999);
          $sms = Doctrine::getTable('SmsMessage')->createQuery()->where('phone = ?',$phone)->andWhere('is_use = 0')->orderBy('created_at desc')->fetchOne();
          
          if($sms){
                $time = time();
                $created_time = strtotime($sms->getCreatedAt());
                if( ($time - $created_time) > 60 ){ //间隔60秒
                    $msg = $this->send_sms($code,$phone);
                    return $this->renderText(json_encode($msg));
                }else{
                    $msg['error'] = 1;
                    $msg['msg'] = '验证码发送失败！验证码1分钟内不能重复发送';
                    return $this->renderText(json_encode($msg));
                }
            }else{
                $msg = $this->send_sms($code,$phone);
                return $this->renderText(json_encode($msg));
            }  
        }
    }
  }
  
    protected function send_sms($code,$phone){
            $msg = '短信验证码：'.$code.'，请在5分钟内完成验证【有学教育】';
            $url = "http://sms.bechtech.cn/Api/send/data/json?accesskey=3585&secretkey=29751faf48356fb629fb902fceba4ed4a83415d3&mobile=".$phone."&content=".urlencode($msg);  
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $dom = curl_exec($ch);
            curl_close($ch);
            $arr = json_decode($dom,true); //格式化返回数组
            $msg = array();

            if($arr['result']=='01'){
                $sms = new SmsMessage();
                $sms->setPhone($phone)->setCode($code)->setIsUse(0)->save();
                $msg['error'] = 0;
                $msg['msg'] = '验证码发送成功！请查收';
                return $msg;
            }else{
                $msg['error'] = 1;
                $msg['msg'] = '验证码发送失败！请与管理员联系';
                return $msg;
            }
  }
}
